<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use  Laravel\Scout\Searchable;
class Post extends Model
{
    use Searchable;
    //允许对['title','content']这些字段进行批量赋值
    protected $fillable = ['title','content','user_id'];

    public function searchableAs()
    {
        return 'post';
    }
    //定义有哪些字段
    public function toSearchableArray(){
        return [
            'title' => $this->title,
            'content' => $this->content
        ];
    }

    //关联用户表，多对一关系，多篇文章对应一个用户
    public function user(){
        return $this->belongsTo('App\User','user_id','id');
    }

    //评论
    public function comments(){
        return $this->hasMany('App\Comment')->orderBy('created_at','desc');
    }

    public function zan($user_id){
        return $this->hasOne(\App\Zan::class)->where('user_id',$user_id);
    }

    public function zans(){
        return $this->hasMany(\App\Zan::class);
    }
}
